package com.songlanyun.modules.rabbitmq.publicbenefitmsg.impl;

import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.donaterecord.service.DonateRecordService;
import com.songlanyun.modules.donation.entity.DonationEntity;
import com.songlanyun.modules.donation.service.DonationService;
import com.songlanyun.modules.donationpoints.entity.DonationPointsEntity;
import com.songlanyun.modules.donationpoints.service.DonationPointsService;
import com.songlanyun.modules.officialDonation.entity.OfficialDonationEntity;
import com.songlanyun.modules.officialDonation.service.OfficialDonationService;
import com.songlanyun.modules.pointsexchangeconfig.entity.PointsExchangeConfigEntity;
import com.songlanyun.modules.pointsexchangeconfig.service.PointsExchangeConfigService;
import com.songlanyun.modules.publicbenefitmsg.model.DonateMsg;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import com.songlanyun.modules.rabbitmq.publicbenefitmsg.IPublicBenefitMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 捐款消息
 */
@Service("donateMsgService")
public class DonateMsgServiceImpl implements IPublicBenefitMsgService {

    @Resource
    private OfficialDonationService officialDonationService;

    @Resource
    private DonationPointsService donationPointsService;

    @Resource
    private DonationService donationService;

    @Resource
    private PublicBenefitMsgService publicBenefitMsgService;

    @Resource
    private PointsExchangeConfigService pointsExchangeConfigService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private DonateRecordService donateRecordService;

    @Override
    public void saveMsg(PublicBenefitMsgTO to) {
        //保存公益记录
        DonateMsg donateMsg = new DonateMsg();
        donateMsg.setModelType(to.getModelType());
        donateMsg.setModelId(to.getModelId());

        //消息
        Long userId = null;
        BigDecimal points = BigDecimal.ZERO;
        String content = null;

        //捐赠
        DonationConstant.DonateMsgType donateType = null;
        String donateContent = null;

        switch (donateMsg.getModelType()) {
            case OFFICIAL_DONATION:
                OfficialDonationEntity officialDonationEntity = officialDonationService.getById(to.getModelId());
                if (ObjectUtil.isNull(officialDonationEntity)) {
                    return;
                }
                content = "捐赠了" + officialDonationEntity.getDonationAmount() + "元现金，获得了" + officialDonationEntity.getIssuedQuantity() + "个公益积分";
                userId = officialDonationEntity.getUserId();
                points = officialDonationEntity.getIssuedQuantity();

                donateType = DonationConstant.DonateMsgType.OFFICIAL_DONATION;
                donateContent = "捐赠了" + officialDonationEntity.getDonationAmount() + "元现金";

                donateMsg.setWelfareType(DonationConstant.WelfareType.DONATION);
                donateMsg.setPoints(points);

                //保存消息
                publicBenefitMsgService.add(
                        userId,
                        DonationConstant.MsgType.DONATE,
                        donateMsg,
                        content
                );

                //保存捐赠记录
                donateRecordService.add(userId, donateType, donateContent);

                break;
            case CHARITABLE_DONATIONS:
                DonationPointsEntity donationPointsEntity = donationPointsService.getById(to.getModelId());
                if (ObjectUtil.isNull(donationPointsEntity)) {
                    return;
                }
                content = "捐赠了" + donationPointsEntity.getDonationPoints() + "个公益积分";
                userId = donationPointsEntity.getDonorId();
                points = donationPointsEntity.getDonationPoints();

                donateType = DonationConstant.DonateMsgType.CHARITABLE_DONATIONS;
                donateContent = content;

                donateMsg.setWelfareType(DonationConstant.WelfareType.DONATE_POINTS);
                donateMsg.setPoints(points);

                //保存消息
                publicBenefitMsgService.add(
                        userId,
                        DonationConstant.MsgType.DONATE,
                        donateMsg,
                        content
                );

                //保存捐赠记录
                donateRecordService.add(userId, donateType, donateContent);
                break;
            case PUBLIC_WELFARE_DONATION:
                DonationEntity donationEntity = donationService.getById(to.getModelId());
                if (ObjectUtil.isNull(donationEntity)) {
                    return;
                }

                //获得的公益积分
                BigDecimal donationExchangeRatio = BigDecimal.ZERO;
                PointsExchangeConfigEntity config = pointsExchangeConfigService.loadConfig(false);
                if (ObjectUtil.isNotNull(config) && ObjectUtil.isNotNull(config.getDonationExchangeRatio())) {
                    donationExchangeRatio = ObjectUtil.isNull(config.getDonationExchangeRatio()) ? BigDecimal.ZERO : config.getDonationExchangeRatio();
                }

                userId = donationEntity.getUserId();
                donateType = DonationConstant.DonateMsgType.PUBLIC_WELFARE_DONATION;

                //捐款
                if (donationEntity.getIsDonation()) {
                    //组装消息内容
                    StringBuilder builder = new StringBuilder();
                    builder.append("捐赠了").append(donationEntity.getFactInterestAmount()).append("元现金");

                    BigDecimal donationPoints = MathUtils.multiply(donationEntity.getFactInterestAmount(), donationExchangeRatio);
                    if (donationPoints.compareTo(BigDecimal.ZERO) > 0) {
                        builder.append("，获得了").append(donationPoints).append("个公益积分");
                    }
                    content = donateContent = builder.toString();

                    donateMsg.setWelfareType(DonationConstant.WelfareType.DONATION);
                    donateMsg.setPoints(donationPoints);
                    //保存消息
                    publicBenefitMsgService.add(
                            userId,
                            DonationConstant.MsgType.DONATE,
                            donateMsg,
                            content
                    );
                    //保存捐赠记录
                    donateRecordService.add(userId, donateType, donateContent);
                }

                //捐物
                if (donationEntity.getIsOffering()) {
                    //组装消息内容
                    StringBuilder builder = new StringBuilder();
                    builder.append("捐赠了价值").append(donationEntity.getFactItemAmount()).append("元的物质");

                    BigDecimal offeringPoints = MathUtils.multiply(donationEntity.getFactItemAmount(), donationExchangeRatio);
                    if (offeringPoints.compareTo(BigDecimal.ZERO) > 0) {
                        builder.append("，获得了").append(offeringPoints).append("个公益积分");
                    }
                    content = donateContent = builder.toString();

                    donateMsg.setWelfareType(DonationConstant.WelfareType.OFFERING);
                    donateMsg.setPoints(offeringPoints);
                    //保存消息
                    publicBenefitMsgService.add(
                            userId,
                            DonationConstant.MsgType.DONATE,
                            donateMsg,
                            content
                    );
                    //保存捐赠记录
                    donateRecordService.add(userId, donateType, donateContent);
                }

                break;
            default:
        }

    }
}
